package com.ytkj.digitalConstruction.service.permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ytkj.digitalConstruction.dao.permission.MenuMapper;
import com.ytkj.digitalConstruction.dao.permission.MenuOperationMapper;
import com.ytkj.digitalConstruction.dao.permission.OperationMapper;
import com.ytkj.digitalConstruction.entity.permission.Menu;
import com.ytkj.digitalConstruction.entity.permission.MenuOperation;
import com.ytkj.digitalConstruction.entity.permission.Operation;
import com.ytkj.digitalConstruction.entity.permission.Permission;
import com.ytkj.digitalConstruction.enums.PermissionType;
import com.ytkj.digitalConstruction.httpbean.permission.ResMenu;
import com.ytkj.digitalConstruction.httpbean.permission.ResOperation;

/**
 * 菜单业务处理类
 * 
 * @author Pete
 * @Date 2017年12月13日
 */
@Service
public class MenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private OperationMapper operationMapper;

	@Autowired
	private MenuOperationMapper menuOperationMapper;

	@Autowired
	private PermissionService permissionService;

	/** 菜单根节点 */
	private final static int ROOT_MENU = 0;

	/**
	 * 获取用户菜单权限
	 * 
	 * @param userId
	 * @return
	 */
	public List<ResMenu> getUserPermissions(int userId) {
		List<ResMenu> rms = getFullMenuTree();
		Map<Integer, Permission> ps = getMenuPermission(userId);
		for (ResMenu rm : rms) {
			if (ps.containsKey(rm.getId())) {
				rm.getOperation().setHave(true);
			}

		}
		return rms;
	}

	/**
	 * 根据用户菜单权限修改菜单权限树
	 * 
	 * @param rms
	 * @param ps
	 * @return
	 */
	public List<ResMenu> updateUserPermissions(List<ResMenu> rms, Map<Integer, Permission> ps) {
		for (ResMenu rm : rms) {
			if (ps.containsKey(rm.getId())) {
				rm.getOperation().setHave(true);
			}
			List<ResMenu> rml = rm.getMenus();
			if (rml != null && rml.size() > 0) {
				rm.setMenus(updateUserPermissions(rml, ps));
			}
		}
		return rms;
	}

	/**
	 * 删除不可用的用户菜单权限
	 * 
	 * @param rms
	 * @return
	 */
	public List<ResMenu> deleteUnavailableUserPermissions(List<ResMenu> rms) {
		for (ResMenu rm : rms) {
			ResOperation ro = rm.getOperation();
			if (ro != null && ro.isHave()) {
				continue;
			}
			List<ResMenu> rml = rm.getMenus();
			if (rml != null && rml.size() > 0) {
				rm.setMenus(deleteUnavailableUserPermissions(rml));
			} else if (ro == null || !ro.isHave()) {
				rms.remove(rm);
			}
		}
		return rms;
	}

	/**
	 * 获取用户菜单权限
	 * 
	 * @param userId
	 * @return
	 */
	public Map<Integer, Permission> getMenuPermission(int userId) {
		Map<Integer, Permission> pm = new HashMap<>();
		List<Permission> ps = permissionService.getUserOperations(userId).get(PermissionType.MENU);
		for (Permission p : ps) {
			if (p.getPermissionType() != PermissionType.MENU.getType()) {
				continue;
			}
			pm.put(p.getId(), p);
		}
		return pm;
	}

	/**
	 * 获取全部菜单操作
	 * 
	 * @return
	 */
	public List<ResMenu> getFullMenuTree() {
		return getMenuTree(ROOT_MENU);
	}

	/**
	 * 获取菜单操作树
	 * 
	 * @param parentId
	 * @return
	 */
	public List<ResMenu> getMenuTree(int parentId) {
		List<ResMenu> list = new ArrayList<>();
		List<Menu> menus = menuMapper.selectByParentId(parentId);
		for (Menu m : menus) {
			ResMenu rm = new ResMenu(m);
			List<MenuOperation> mos = menuOperationMapper.selectByMenuId(m.getMenuId());
			if (mos != null) {
				for (MenuOperation mo : mos) {
					Operation o = operationMapper.selectByPrimaryKey(mo.getOperationId());
					ResOperation ro = new ResOperation(o);
					ro.setHave(false);
					rm.setOperation(ro);
				}
			}
			rm.setMenus(getMenuTree(m.getMenuId()));
		}
		return list;
	}

	/**
	 * 根据关联ID查询菜单操作
	 * 
	 * @param id
	 * @return
	 */
	public Map<Integer, Map<String, String>> getMenuOperation(int id) {
		Map<Integer, Map<String, String>> map = new HashMap<>();
		MenuOperation menuOperation = menuOperationMapper.selectByPrimaryKey(id);
		Menu menu = menuMapper.selectByPrimaryKey(menuOperation.getMenuId());
		Operation operation = operationMapper.selectByPrimaryKey(menuOperation.getOperationId());
		Map<String, String> ops = new HashMap<>();
		ops.put("id", operation.getOperationId() + "");
		ops.put("name", operation.getOperationName());
		ops.put("url", operation.getOperationUrl());
		ops.put("desc", operation.getOperationDesc());
		ops.put("have", "false");
		map.put(menu.getMenuId(), ops);
		return map;
	}

	/**
	 * 获取菜单信息
	 * 
	 * @param menuId
	 * @return
	 */
	public Menu getMenu(int menuId) {
		return menuMapper.selectByPrimaryKey(menuId);
	}

	/**
	 * 根据关联ID查询菜单操作
	 * 
	 * @param id
	 * @return
	 */
	public ResMenu getResMenuById(int id) {
		MenuOperation menuOperation = menuOperationMapper.selectByPrimaryKey(id);
		if (menuOperation != null) {
			Menu menu = menuMapper.selectByPrimaryKey(menuOperation.getMenuId());
			Operation operation = operationMapper.selectByPrimaryKey(menuOperation.getOperationId());
			if (menu != null && operation != null) {
				ResOperation resOperation = new ResOperation(operation);
				resOperation.setHave(false);
				ResMenu resMenu = new ResMenu(menu);
				resMenu.setOperation(resOperation);
				return resMenu;
			}
		}
		return null;
	}

}
